#!/bin/bash

export JAVA_HOME={{ jdk_home }}
export PATH=$JAVA_HOME/bin:$PATH

function nacos_start(){
    nacos_status=`ps aux |grep nacos.nacos |grep java |wc -l`
    if [[ nacos_status -eq 0 ]];then
        sh {{ nacos_basedir }}/bin/startup.sh -m standalone > /dev/null 2>&1 &
        sleep 2
	nacos_status1=`ps aux |grep nacos.nacos |grep java |wc -l`
	if [[ nacos_status1 -eq 0 ]];then
            echo "start nacos failed"
			exit 1
		else
			echo "start nacos success"
			exit 0
		fi
	else
        echo "nacos already running"
		exit 1
    fi
}



function nacos_stop(){
    nacos_status=`ps aux |grep nacos.nacos |grep java |wc -l`
    if [[ nacos_status -gt 0 ]];then
        sh {{ nacos_basedir }}/bin/shutdown.sh
        sleep 2
    else
        echo "nacos not running"
	exit 1
    fi
}



function nacos_status(){
    nacos_status=`ps aux |grep nacos.nacos |grep java |wc -l`
    if [[ nacos_status -gt 0 ]];then
		echo "nacos already running"
	else
		echo "nacos not running"
    fi
}



case $1 in
        start)
            nacos_start
        ;;
        stop)
            nacos_stop
        ;;
        status)
            nacos_status
        ;;
		*)
		     echo "Usage $0 {start|stop|status}"
		;;
esac
